<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="菜单列表"
      row-key="_id"
      :pagination="false"
      :indent="20"
      :columns="columns"
      :highlight-current-row="true"
      :request-api="getTableList"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增 </el-button>
        <el-button
          type="primary"
          plain
          :icon="CirclePlus"
          :disabled="!scope.isSelected"
          @click="openDrawer('新增下级', {}, scope.selectedList)"
        >
          新增下级
        </el-button>
      </template>
      <!-- 菜单图标 -->
      <template #icon="scope">
        <el-icon :size="18">
          <component :is="scope.row.meta.icon"></component>
        </el-icon>
      </template>
      <!-- 菜单操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)"> 编辑 </el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteData(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <RoleDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="menuMange">
import { ref } from "vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, CirclePlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import apiService from "@/api/services/apiService";
import RoleDrawer from "./roleDrawer.vue";
import { Roles } from "@/api/types/base";
import { useHandleData } from "@/hooks/useHandleData";
import { dateRender } from "@/hooks/useColumnRender";

const proTable = ref();

const getTableList = (params: any) => {
  // let newParams = JSON.parse(JSON.stringify(params));
  return apiService.findTree("roles", params.condition, params.options);
};

// 表格配置项
const columns: ColumnProps[] = [
  { prop: "name", label: "角色名称", align: "left", search: { el: "input" } },
  { prop: "note", label: "角色说明", align: "left", search: { el: "input" } },
  { prop: "key", label: "角色KEY", width: 200, align: "left", search: { el: "input" } },
  { prop: "createUser", label: "创建用户", width: 100 },
  { prop: "createDate", label: "创建时间", render: dateRender },
  { prop: "operation", label: "操作", width: 180, fixed: "right" }
];

// 删除用户信息
const deleteData = async (params: Roles) => {
  await useHandleData(apiService.deleteById, "roles", params._id, `删除角色【${params.name}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof RoleDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Roles> = {}, selectList: { [key: string]: any }[] = []) => {
  if (title.indexOf("新增") !== -1) {
    selectList?.length ? (row.pid = selectList[0]._id) : delete row.pid;
  }
  const params = {
    title,
    tbName: "roles",
    isView: title === "查看",
    row: { ...row },
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
